package cn.itcast.n3_Java线程;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * <p>
 * Test01_创建线程4_实现Callable接口
 * </p>
 *
 * @author flyduck
 * @since 2024-08-21
 */
@Slf4j(topic = "c.Test01_创建线程4_实现Callable接口")
public class Test01_创建线程4_实现Callable接口 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<Integer> futureTask = new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                log.debug("running...");
                Thread.sleep(2000);
                return 100;
            }
        });

        Thread t1 = new Thread(futureTask,"t1");
        t1.start();

        //get()方法一直等待结果返回
        log.debug("结果：{}",futureTask.get());
    }
}
